<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/5/26
 * Time: 14:40
 */

namespace AdminsBundle\Controller;

use AdminsBundle\Entity\ProductShow as Entity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use AdminsBundle\Helper\MyController;

class ProductShowController extends MyController{

    /*快速复制项目通用化配置*/
    private $entityName = "ProductShow";

    /**
     * @route("ProductShow/edit/{id}",name="ProductShowPreEdit")
     */
    public function edit(Entity $entity)
    {
        $action = $this->generateUrl($this->entityName."DoEdit",array("id"=>$entity->getId()));
        $form = $this->createMyForm($this->entityName ,  $entity ,  $action );
        $show = $form->createView();
        $data = array(
            "title" => "产品详情",
            "submitType" => "Post",
            "show"   => $show,
        );
        /*非公共部分*/

        $intro = json_decode($entity->getIntro(),true);
        if (!empty($intro)) {
            $picsListsFirstPic = $intro[0];
            unset($intro[0]);
            $picsLists = $intro;
            $data["picsListsFirstPic"] = $picsListsFirstPic;
            $data["picsLists"] = $picsLists;
        } else  {
            $data["picsListsFirstPic"] = "未上传";
            $data["picsLists"] = array();
        }
        /*非公共部分*/
        return $this->render("AdminsBundle:".$this->entityName.":add.php.twig",$data);
    }

    /**
     * @route("ProductShow/edit/{id}/do",name="ProductShowDoEdit")
     *
     * @Method({"GET", "POST"})
     */
    public function doEdit(Request $request,Entity $entity)
    {
        $form = $this->createMyForm($this->entityName ,  $entity );
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $picsLists = $request->get("picsLists");
            if (is_array($picsLists)) {
                $intro = json_encode($picsLists);
                $entity->setIntro($intro);
            } else {
                $entity->setIntro("未上传");
            }
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();
            return $this->redirectToRoute("ProductLists");
        } else {
            return $this->js_out($this->generateUrl($this->entityName."PreEdit",array("id"=>$entity->getId())),"系统错误，请联系管理员");
        }
    }

}